package com.huawei.hms.framework.network.restclient;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.hwhttp.ClientConfiguration;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestBody;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;

public abstract class ParameterBuilder<T> {
    private static final String TAG = "ParameterBuilder";

    static final class Body<T> extends ParameterBuilder<T> {
        private final Converter<T, RequestBody> converter;

        Body(Converter<T, RequestBody> converter) {
            this.converter = converter;
        }

        void build(RequestBuilder requestBuilder, @Nullable T t) {
            if (t == null) {
                ParameterBuilder.throwParamValueError("Body parameter value must not be null.");
            }
            try {
                requestBuilder.setBody((RequestBody) this.converter.convert(t));
            } catch (Throwable e) {
                throw new RuntimeException("Unable to convert " + t + " to RequestBody", e);
            }
        }
    }

    public static final class ClientConfig extends ParameterBuilder<ClientConfiguration> {
        void build(RequestBuilder requestBuilder, ClientConfiguration clientConfiguration) {
            requestBuilder.setClientConfig(clientConfiguration);
        }
    }

    static final class FieldMap<T> extends ParameterBuilder<Map<String, T>> {
        private final Converter<T, String> converter;

        FieldMap(Converter<T, String> converter) {
            this.converter = converter;
        }

        void build(RequestBuilder requestBuilder, @Nullable Map<String, T> map) throws IOException {
            if (map == null) {
                ParameterBuilder.throwParamValueError("Field map was null.");
            }
            for (Entry entry : map.entrySet()) {
                String str = (String) entry.getKey();
                if (str == null) {
                    ParameterBuilder.throwParamValueError("Field map contained null key.");
                }
                Object value = entry.getValue();
                if (value == null) {
                    ParameterBuilder.throwParamValueError("Field map contained null value for key '" + str + "'.");
                }
                String str2 = (String) this.converter.convert(value);
                if (str2 == null) {
                    ParameterBuilder.throwParamValueError("Field map value '" + value + "' converted to null by " + this.converter.getClass().getName() + " for key '" + str + "'.");
                }
                requestBuilder.addFormField(str, str2);
            }
        }
    }

    static final class FieldParam<T> extends ParameterBuilder<T> {
        private final Converter<T, String> converter;
        private final String name;

        FieldParam(String str, Converter<T, String> converter) {
            if (str == null) {
                ParameterBuilder.throwParamValueError("Field parameter name must be not null.");
            }
            this.name = str;
            this.converter = converter;
        }

        void build(RequestBuilder requestBuilder, @Nullable T t) throws IOException {
            if (t != null) {
                String str = (String) this.converter.convert(t);
                if (str != null) {
                    requestBuilder.addFormField(this.name, str);
                }
            }
        }
    }

    static final class Header<T> extends ParameterBuilder<T> {
        private final Converter<T, String> converter;
        private final String name;

        Header(String str, Converter<T, String> converter) {
            if (str == null) {
                ParameterBuilder.throwParamValueError("Header parameter name must be not null.");
            }
            this.name = str;
            this.converter = converter;
        }

        void build(RequestBuilder requestBuilder, T t) throws IOException {
            if (t != null) {
                String str = (String) this.converter.convert(t);
                if (str != null) {
                    requestBuilder.addHeader(this.name, str);
                }
            }
        }
    }

    static final class HeaderMap<T> extends ParameterBuilder<Map<String, T>> {
        private final Converter<T, String> converter;

        HeaderMap(Converter<T, String> converter) {
            this.converter = converter;
        }

        void build(RequestBuilder requestBuilder, @Nullable Map<String, T> map) throws IOException {
            if (map == null) {
                ParameterBuilder.throwParamValueError("Header map was null.");
            }
            for (Entry entry : map.entrySet()) {
                String str = (String) entry.getKey();
                if (str == null) {
                    ParameterBuilder.throwParamValueError("Header map contained null key.");
                }
                Object value = entry.getValue();
                if (value == null) {
                    ParameterBuilder.throwParamValueError("Header map contained null value for key '" + str + "'.");
                }
                requestBuilder.addHeader(str, (String) this.converter.convert(value));
            }
        }
    }

    static final class PathParam<T> extends ParameterBuilder<T> {
        private final Converter<T, String> converter;
        private final String name;

        PathParam(String str, Converter<T, String> converter) {
            if (str == null) {
                ParameterBuilder.throwParamValueError("Path parameter name must be not null.");
            }
            this.name = str;
            this.converter = converter;
        }

        void build(RequestBuilder requestBuilder, T t) throws IOException {
            if (t == null) {
                ParameterBuilder.throwParamValueError("Path parameter \"" + this.name + "\" value must not be null.");
            }
            requestBuilder.addPathParam(this.name, (String) this.converter.convert(t));
        }
    }

    static final class QueryMap<T> extends ParameterBuilder<Map<String, T>> {
        private final Converter<T, String> converter;

        QueryMap(Converter<T, String> converter) {
            this.converter = converter;
        }

        void build(RequestBuilder requestBuilder, @Nullable Map<String, T> map) throws IOException {
            if (map == null) {
                ParameterBuilder.throwParamValueError("Query map was null.");
            }
            for (Entry entry : map.entrySet()) {
                String str = (String) entry.getKey();
                if (str == null) {
                    ParameterBuilder.throwParamValueError("Query map contained null key.");
                }
                Object value = entry.getValue();
                if (value == null) {
                    ParameterBuilder.throwParamValueError("Query map contained null value for key '" + str + "'.");
                }
                String str2 = (String) this.converter.convert(value);
                if (str2 == null) {
                    ParameterBuilder.throwParamValueError("Query map value '" + value + "' converted to null by " + this.converter.getClass().getName() + " for key '" + str + "'.");
                }
                requestBuilder.addQueryParam(str, str2);
            }
        }
    }

    static final class QueryParam<T> extends ParameterBuilder<T> {
        private final String name;
        private final Converter<T, String> valueConverter;

        QueryParam(String str, Converter<T, String> converter) {
            if (str == null) {
                ParameterBuilder.throwParamValueError("Query parameter name must be not null.");
            }
            this.name = str;
            this.valueConverter = converter;
        }

        void build(RequestBuilder requestBuilder, T t) throws IOException {
            if (t != null) {
                String str = (String) this.valueConverter.convert(t);
                if (str != null) {
                    requestBuilder.addQueryParam(this.name, str);
                }
            }
        }
    }

    static final class RelativeUrl extends ParameterBuilder<Object> {
        RelativeUrl() {
        }

        void build(RequestBuilder requestBuilder, Object obj) {
            if (obj == null) {
                ParameterBuilder.throwParamValueError("@Url parameter is null.");
            }
            if (obj instanceof String) {
                requestBuilder.setRelativeUrl((String) obj);
            } else {
                ParameterBuilder.throwParamValueError("@Url parameter must be String.");
            }
        }
    }

    abstract void build(RequestBuilder requestBuilder, T t) throws IOException;

    final ParameterBuilder<Object> array() {
        return new ParameterBuilder<Object>() {
            void build(RequestBuilder requestBuilder, Object obj) throws IOException {
                if (obj == null) {
                    Logger.w(ParameterBuilder.TAG, "ParameterBuilder.array.build failed, values == null");
                    return;
                }
                int length = Array.getLength(obj);
                for (int i = 0; i < length; i++) {
                    ParameterBuilder.this.build(requestBuilder, Array.get(obj, i));
                }
            }
        };
    }

    final ParameterBuilder<Iterable<T>> iterable() {
        return new ParameterBuilder<Iterable<T>>() {
            void build(RequestBuilder requestBuilder, Iterable<T> iterable) throws IOException {
                if (iterable == null) {
                    Logger.w(ParameterBuilder.TAG, "ParameterBuilder.iterable.build failed, values == null");
                    return;
                }
                for (T build : iterable) {
                    ParameterBuilder.this.build(requestBuilder, build);
                }
            }
        };
    }

    private static void throwParamValueError(String str) {
        throw new IllegalArgumentException(str);
    }
}
